package com.hcbd.tdwt.ui.xingyu.fragment;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.hcbd.tdwt.R;
import com.hcbd.tdwt.base.BaseFragment;
import com.hcbd.tdwt.base.Listener.InfoInterface;
import com.hcbd.tdwt.base.bd.BdToolFactory;
import com.hcbd.tdwt.util.DateUtils;
import com.hcbd.tdwt.view.GnssSkyView;
import com.pangu.bdsdk2021.entity.terminaltwo.BDGGAInfo;
import com.pangu.bdsdk2021.entity.terminaltwo.BDRMCInfo;
import com.hcbd.tdwt.ui.xingyu.vo.SatelliteVO;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

import java.lang.ref.WeakReference;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

@SuppressLint("NonConstantResourceId")
@EFragment(R.layout.fragment_sky_map)
public class SkyMapFragment extends BaseFragment implements InfoInterface {
    @ViewById
    GnssSkyView gnssSkyView;

    @ViewById
    TextView mixTotal,usaTotal,russiaTotal,euTotal,china_total;
    @ViewById
    TextView time;
    @ViewById
    RelativeLayout bgRL;
    @ViewById
    LinearLayout chinaLL,usaLL,russiaLL,euLL,mixLL;
    public int mixIndex,usaIndex,russiaIndex,euIndex,chinaIndex;
    private  Timer timer=new Timer();
    private TimerTask task;
    private MyHandler myHandler;
    private static class MyHandler extends Handler {
        //弱引用，在垃圾回收时，被回收
        WeakReference<SkyMapFragment> fragmentWeakReference;
        MyHandler(SkyMapFragment fragment){
            this.fragmentWeakReference=new WeakReference<>(fragment);
        }
        public void handleMessage(Message msg){
            fragmentWeakReference.get().time.setText(DateUtils.getDateLongSerial());
        }
    }

    @AfterViews
    protected void init() {
        startTime();
        if(BdToolFactory.app_type!=BdToolFactory.XY_BG_TYPE&&BdToolFactory.app_type!=BdToolFactory.XY_PDA_BG_TYPE){
            bgRL.setBackground(getContext().getDrawable(R.color.white));
            gnssSkyView.setBgColor(getContext().getColor(R.color.black));
            gnssSkyView.invalidate();
            time.setTextColor(getContext().getColor(R.color.black));
            mixTotal.setTextColor(getContext().getColor(R.color.black));
            usaTotal.setTextColor(getContext().getColor(R.color.black));
            russiaTotal.setTextColor(getContext().getColor(R.color.black));
            euTotal.setTextColor(getContext().getColor(R.color.black));
            china_total.setTextColor(getContext().getColor(R.color.black));
            chinaLL.setBackground(getContext().getDrawable(R.drawable.layout_frame_while_ff));
            usaLL.setBackground(getContext().getDrawable(R.drawable.layout_frame_while_ff));
            russiaLL.setBackground(getContext().getDrawable(R.drawable.layout_frame_while_ff));
            euLL.setBackground(getContext().getDrawable(R.drawable.layout_frame_while_ff));
            mixLL.setBackground(getContext().getDrawable(R.drawable.layout_frame_while_ff));
        }
    }
    public void startTime() {
        myHandler=new MyHandler(this);
        task = new TimerTask() {
            @Override
            public void run() {
                Message message = new Message();
                myHandler.sendMessage(message);
            }
        };
        timer.schedule(task, 1000, 1000);
    }
    @Override
    public void onGSVInfo(List<SatelliteVO> satelliteVOS) {
        chinaIndex=0;
        usaIndex=0;
        russiaIndex=0;
        euIndex=0;
        mixIndex=0;
        if(gnssSkyView!=null){
            for(SatelliteVO satellite:satelliteVOS){
                if(satellite.getSatelliteType().contains("BD")||satellite.getSatelliteType().contains("PQ")||satellite.getSatelliteType().contains("GB")){
                    chinaIndex+=1;
                }else if(satellite.getSatelliteType().contains("GP")){
                    usaIndex+=1;
                }else if(satellite.getSatelliteType().contains("GL")){
                    russiaIndex+=1;
                }else if(satellite.getSatelliteType().contains("GA")){
                    euIndex+=1;
                }else{
                    mixIndex+=1;
                }
            }
            usaTotal.setText(String.valueOf(usaIndex));
            russiaTotal.setText(String.valueOf(russiaIndex));
            euTotal.setText(String.valueOf(euIndex));
            china_total.setText(String.valueOf(chinaIndex));
            mixTotal.setText(String.valueOf(mixIndex));
            gnssSkyView.drawBatchSatelliteVO(satelliteVOS);
        }

    }

    @Override
    public void onGGAInfo(BDGGAInfo bdggaInfo) {

    }

    @Override
    public void onRMCInfo(BDRMCInfo bdrmcInfo) {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if(timer!=null){
            timer.cancel();
            task.cancel();
            timer=null;
        }
        if(myHandler!=null){
            myHandler.removeCallbacksAndMessages(null);
        }

    }
}
